//
//  FYHTypeClass.m
//  FollowYourHeart
//
//  Created by lanou3g on 16/3/12.
//  Copyright © 2016年 李晨玲. All rights reserved.
//

#import "FYHTypeClass.h"
#import <Masonry.h>
#import "FYHWineshop.h"
#import "FYHBuying.h"
#import "TYHRoom.h"
#import "FYHScenic.h"


static const CGFloat TitleH = 50;
@interface FYHTypeClass ()<UITableViewDelegate,UIScrollViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)UITableView *tableV;
@property(nonatomic,strong)UIScrollView *scrollV;
@property(nonatomic,strong)UIScrollView *scrollVMiddle;
@property(nonatomic,strong)NSArray *titleArr; /* 标题栏 */
@property(nonatomic,strong)UIButton *selectedBtn;
@property(nonatomic,strong)NSMutableArray *buttons;
@property(nonatomic,strong)UIView *downView;


@end

@implementation FYHTypeClass
- (NSArray *)titleArr{
    if (!_titleArr) {
        _titleArr = [[NSArray alloc] init];
        self.titleArr = @[@"景点",@"餐厅",@"酒店",@"购物"];
    }
    return _titleArr;
}
- (NSMutableArray *)buttons{
    if (!_buttons) {
        _buttons = [[NSMutableArray alloc] init];
    }
    return _buttons;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    NSLog(@"%ld",self.btnTarge);
    self.tableV.delegate = self;
    self.tableV.dataSource = self;
    
    [self addChildView];    /** 创建子视图控制器*/
    [self setViews];       /**   创建视图     */
    [self setDatas];     /** 拿到数据        */
    
    
}

- (void)setDatas{
    
    
}
- (void)addChildView{
    FYHBuying *buy          = [[FYHBuying alloc] init];
    FYHScenic *scenic       = [[FYHScenic alloc] init];
    FYHWineshop *wineshop = [[FYHWineshop alloc] init];
    TYHRoom *room             = [[TYHRoom alloc] init];
    
    [self addChildViewController:buy];
    [self addChildViewController:scenic];
    [self addChildViewController:wineshop];
    [self addChildViewController:room];
}



#pragma mark - 页面布局
- (void)setViews{
    [self setUpView];
    [self setTitleView];
    [self setMiddleView];
    [self setTableview];
    
}
- (void)setUpView{

    UIView *viewUp = [[UIView alloc] init];
    [self.view addSubview:viewUp];
    UIButton *buttonLeft = [[UIButton alloc] init];
    [buttonLeft addTarget:self action:@selector(buttonLeftAction) forControlEvents:(UIControlEventTouchDown)];
    [buttonLeft setBackgroundImage:[UIImage imageNamed:@"iconfont-fanhui.png"] forState:(UIControlStateNormal)];
    [viewUp addSubview:buttonLeft];
    UIButton *buttonright = [UIButton new];
    [buttonright addTarget:self action:@selector(buttonRightAction) forControlEvents:(UIControlEventTouchDown)];
    [buttonright setBackgroundImage:[UIImage imageNamed:@"iconfont-ditu.png"] forState:(UIControlStateNormal)];
    [viewUp addSubview:buttonright];
    UILabel *labletitle = [[UILabel alloc] init];
#warning 唯一的参数
    labletitle.text = @"曼谷酒店";
    [viewUp addSubview:labletitle];
    [viewUp mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(@0);
        make.right.mas_equalTo(@0);
        make.top.mas_equalTo(@0);
        make.height.mas_equalTo(TitleH);
    }];
    [buttonLeft mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(viewUp).with.offset(10);
        make.centerY.equalTo(viewUp);
        make.size.mas_equalTo(CGSizeMake(40, TitleH-14));
    }];
    [buttonright mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.mas_equalTo(viewUp).with.offset(-10);
        make.centerY.equalTo(viewUp);
        make.size.mas_equalTo(CGSizeMake(40, TitleH-14));
        
    }];
    [labletitle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.height.mas_equalTo(TitleH);
        
        
    }];
}
- (void)setTitleView{
    /** scroller 最上面的把标题*/
    self.scrollV = [[UIScrollView alloc] init];
    self.scrollV.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:self.scrollV];
    self.scrollV.scrollEnabled = NO;
    [self.scrollV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(TitleH);
        make.height.mas_equalTo(@30);
        make.width.mas_equalTo(kScreenWidth);
    }];
    UIView *container =[UIView new];
    [self.scrollV addSubview:container];
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.scrollV);
        make.height.equalTo(self.scrollV);
    }];
    int count = 4;
    UIButton *lastButton = nil;
    for (int i = 0; i < count; i++) {
        UIButton *buttonSubv = [[UIButton alloc] init];
        [container addSubview:buttonSubv];
        [buttonSubv setTitle:self.titleArr[i] forState:(UIControlStateNormal)];
        [buttonSubv addTarget:self action:@selector(buttonSubvAction:) forControlEvents:(UIControlEventTouchDown)];
        buttonSubv.tag = i;
        [self.buttons addObject:buttonSubv];//加入button的数组
        if (i == 0) {
            [self setUpOneChiled:i];
        }
        [buttonSubv mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.and.bottom.equalTo(self.scrollV);
            make.width.mas_equalTo(kScreenWidth/4);
            if (lastButton) {
                make.left.mas_equalTo(lastButton.mas_right);
            }
            else
            {
                /** 这里设置lastview是为了设置第一个*/
                make.left.mas_equalTo(container.mas_left);
            }
        }];
        lastButton = buttonSubv;
    }
    [container mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.right.equalTo(lastButton.mas_right);
    }];
}
- (void)setMiddleView{
    self.scrollVMiddle = [[UIScrollView alloc] init];
    [self.view addSubview:self.scrollVMiddle];
    self.scrollVMiddle.backgroundColor = [UIColor redColor];
    self.scrollVMiddle.delegate = self;
    self.scrollVMiddle.showsHorizontalScrollIndicator = YES;
    self.scrollVMiddle.pagingEnabled = YES;
    self.scrollVMiddle.contentSize = CGSizeMake(ScreenW * 4, 0);
    [self.scrollVMiddle mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.scrollV.mas_bottom);
        make.left.equalTo(self.view);
        make.width.mas_equalTo(ScreenW);
#warning 这里底面的约束改成相对于下图
        make.bottom.mas_equalTo(self.view).with.offset(-50);
        
    }];
   /** 底部视图创建*/
    self.downView = [[UIView alloc] init];
    [self.view addSubview:self.downView];
    [self.downView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.and.bottom.equalTo(self.view);
        make.height.mas_equalTo(@50);
    }];
    UIButton *buttonDown1 = [[UIButton alloc] init];
    [self.downView addSubview:buttonDown1];
    UIButton *buttondown2 = [[UIButton alloc] init];
    [self.downView addSubview:buttondown2];
    UILabel *lableDown1  =[[UILabel alloc] init];
    [self.downView addSubview:lableDown1];
    UILabel *lableDown2 = [[UILabel alloc] init];
    [self.downView addSubview:lableDown2];
    lableDown1.text = @"筛选";
    lableDown2.text = @"排序";
    lableDown1.font = [UIFont systemFontOfSize:14];
    lableDown2.font = [UIFont systemFontOfSize:14];
    [buttonDown1 setBackgroundImage:[UIImage imageNamed:@"iconfont-shaixuan.png"] forState:(UIControlStateNormal)];
    [buttondown2 setBackgroundImage:[UIImage imageNamed:@"iconfont-paixu.png"] forState:(UIControlStateNormal)];
    [buttonDown1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.downView.mas_top).with.offset(10);
        make.left.mas_equalTo(self.downView.mas_left).with.offset(50);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [buttondown2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.downView.mas_top).with.offset(10);
        make.right.mas_equalTo(self.downView.mas_right).with.offset(-115);
        make.size.mas_equalTo(CGSizeMake(30, 30));
    }];
    [lableDown1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downView);
        make.left.mas_equalTo(buttonDown1.mas_right).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(40, 60));
    }];
    [lableDown2 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.downView.mas_top);
        make.left.mas_equalTo(buttondown2.mas_right).with.offset(5);
        make.size.mas_equalTo(CGSizeMake(40, 60));
        
    }];
    
}
- (void)setTableview{
#warning 这里加到谁上?
    
//    /** 加到控制器上*/
//    self.tableV = [[UITableView alloc] init];
//    [self.scrollVMiddle addSubview:self.tableV];
//    self.tableV.backgroundColor = [UIColor cyanColor];
//    [self.tableV mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.top.equalTo(self.view);
//        make.bottom.equalTo(self.view);
//        make.left.equalTo(self.view);
//        make.width.mas_equalTo(ScreenW);
//    }];
    
    
    
}
- (void)buttonSubvAction:(UIButton *)sender{
    /** 改变字体颜色 */
    [self.selectedBtn setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    [sender setTitleColor:[UIColor greenColor] forState:(UIControlStateNormal)];
    self.selectedBtn = sender;
     NSInteger i = sender.tag;
    /** 点击button找到对应的scrollMiddle偏移量*/
    self.scrollVMiddle.contentOffset = CGPointMake(ScreenW * i, 0);
    /** 加载对应的子控制器*/
    [self setUpOneChiled:i];
}
- (void)setUpOneChiled:(NSInteger)index{
    
#warning 进来没有加载子视图.
    CGFloat x = index *ScreenW;
    UIViewController *chiledV = self.childViewControllers[index];
    [self.scrollVMiddle addSubview:chiledV.view];
    chiledV.view.backgroundColor = RandomColor;
    if (chiledV.view.superview) {
        return;
    }
    chiledV.view.frame = CGRectMake(x, 0, ScreenW, ScreenH - self.scrollVMiddle.frame.origin.y);
//    [chiledV.view mas_makeConstraints:^(MASConstraintMaker *make) {
//    
//     
//        
//    }];
//    
    NSLog(@"子视图");
}

/** 拖拽 scrollMiddle找到相对应的button*/
-(void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    NSInteger i = self.scrollVMiddle.contentOffset.x/ScreenW;
    [self buttonSubvAction:self.buttons[i]];
    
}

- (void)buttonLeftAction{
    [self dismissViewControllerAnimated:YES completion:^{
        NSLog(@"传值");
        
    }];
    
}
- (void)buttonRightAction{
    [self.scrollV mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(@0.01);
       
    }];
    [self.downView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(0);
    }];
    [self.scrollVMiddle mas_updateConstraints:^(MASConstraintMaker *make) {
        
        make.bottom.equalTo(self.view);
    }];
    
    
}

#pragma mark - tableview 

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    NSLog(@"cell");
    return 100;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSLog(@"ddd");
    static NSString *cellID = @"UITableViewCell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:(UITableViewCellStyleSubtitle) reuseIdentifier:cellID];
    }
    cell.textLabel.text = @"我是个好人";
    return cell;
    
}


/**向上滑,向下滑,显示和隐藏*/
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView{
    NSLog(@"开始拖拽");
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    NSLog(@"滚动");
}














- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
    
}




/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
